package com.neusoft.crm.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.crm.common.R;
import com.neusoft.crm.mapper.SaleChanceMapper;
import com.neusoft.crm.mapper.SysUserMapper;
import com.neusoft.crm.po.SplitPageValues;
import com.neusoft.crm.po.SaleChance;
import com.neusoft.crm.service.ISaleChanceService;
import com.neusoft.crm.service.ISysUserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@Log4j2
public class SaleChanceController {

    @Resource
    private ISaleChanceService saleChanceService;
    @Resource
    private SaleChanceMapper saleChanceMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private ISysUserService sysUserService;



//    @RequestMapping("/selectSaleChanceByStatus")
//    @Transactional
//    public List<SaleChance> selectSaleChanceByStatus(@RequestBody SaleChance saleChance) {
//
//        log.info(saleChanceService.count());
//        return saleChanceService.selectSaleChanceByStatus(saleChance);
//    }

    @RequestMapping("/selectSaleChanceByStatus")
    @Transactional
    public Page<SaleChance> page(@RequestBody SplitPageValues splitPageValues)
    {
        //构造分页构造器
        Page<SaleChance> pageInfo = new Page<>(splitPageValues.getCurrentPage(),splitPageValues.getPageSize());
        QueryWrapper<SaleChance> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("chance_Cust_Name",splitPageValues.getChanceCustName())
                .like("chance_Title",splitPageValues.getChanceTitle())
                .like("chance_Linkman",splitPageValues.getChanceLinkman());

        return  saleChanceService.page(pageInfo, queryWrapper);

    }




    @RequestMapping("/insertSaleChance")
    public int creatSaleChance(@RequestBody SaleChance saleChance) {
        return saleChanceMapper.insert(saleChance);
    }

    @RequestMapping("/saleChanceEdit")
    @Transactional
    public R<String> editSaleChance(@RequestBody SaleChance saleChance) {
          saleChanceService.updateById(saleChance);
        return R.success("更新成功");
    }



    @RequestMapping("/saleChanceAssign")
    public boolean saleChanceAssign(@RequestBody SaleChance saleChance){
        return saleChanceService.updateById(saleChance);
    }

    @RequestMapping("/selectSaleChanceByChanceDueId")
    @Transactional
    public List<SaleChance> selectSaleChanceByChanceDueId(){
        return saleChanceService.selectSaleChanceByChanceDueId();
    }

    @RequestMapping("/saleChanceSuccess")
    public int  saleChanceSuccess(@RequestBody SaleChance saleChance){
        saleChance.setChanceStatus(2);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date currentDate = new Date();
        String formattedDate = sdf.format(currentDate);
        saleChance.setChanceDueDate(formattedDate);
         return saleChanceMapper.updateById(saleChance);
    }

    @RequestMapping("/salechanceFail")
    public boolean salechanceFail(@RequestBody SaleChance saleChance){
        saleChance.setChanceStatus(3);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date currentDate = new Date();
        String formattedDate = sdf.format(currentDate);
        saleChance.setChanceDueDate(formattedDate);

        return saleChanceService.updateById(saleChance);
    }





}



